package com.example.hzq.food.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hzq.food.R;
import com.example.hzq.food.bean.OrderBean;
import com.example.hzq.food.bean.Success;
import com.example.hzq.food.model.Model;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.hzq.food.activity.Login.userid;

/**
 * Created by hzq on 2017/6/28.
 */

public class Orderdetail extends AppCompatActivity implements View.OnClickListener {
    private  static String imagepath;
    private OrderBean orderBean;
    private ImageView foodimage;
    private TextView foodname,num,price,sum,shopname,shopaddress,suggess,username,ordertime,comment;
    private Button add,change,delete;
    private EditText scanf;
    private ImageButton back;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.orderdetail);
        back=(ImageButton)findViewById(R.id.orderdetailback) ;
        foodname=(TextView)findViewById(R.id.orderdetailfoodname);
        foodimage=(ImageView)findViewById(R.id.orderfoodimage) ;
        num=(TextView)findViewById(R.id.orderdetailfoodnum);
        price=(TextView)findViewById(R.id.orderdetailfoodprice);
        sum=(TextView)findViewById(R.id.orderdetailfoodsum);
        shopname=(TextView)findViewById(R.id.orderdetailshopname);
        shopaddress=(TextView)findViewById(R.id.orderdetailshopaddress);
        suggess=(TextView)findViewById(R.id.orderdetailsuggess);
        username=(TextView)findViewById(R.id.orderdetailusername);
        ordertime=(TextView)findViewById(R.id.orderdetailtime);
        comment=(TextView)findViewById(R.id.orderdetailcomment);
        scanf=(EditText)findViewById(R.id.scanfcomment);
        add=(Button)findViewById(R.id.addcomment);
        change=(Button)findViewById(R.id.changecomment);
        delete=(Button)findViewById(R.id.deletecomment);
        add.setOnClickListener(this);
        change.setOnClickListener(this);
        delete.setOnClickListener(this);
        imagepath=getIntent().getStringExtra("imagepath");
        Picasso.with(this).load(imagepath).placeholder(R.drawable.ic_default_adimage).into(foodimage);
        Model model=new Model();
        Call<List<OrderBean>> call= model.getorder(Integer.valueOf(userid));
        call.enqueue(new Callback<List<OrderBean>>() {
            @Override
            public void onResponse(Call<List<OrderBean>> call, Response<List<OrderBean>> response) {
                if(response.isSuccessful()&&response.body()!=null)
                {
                    orderBean=response.body().get(getIntent().getIntExtra("index",0));
                    foodname.setText("菜品名称："+orderBean.getFoodname());
                    num.setText("购买数量："+orderBean.getNum());
                    price.setText("单        价："+orderBean.getPrice());
                    sum.setText("总  价  格"+orderBean.getSum());
                    shopname.setText("店铺名称："+orderBean.getShopname());
                    shopaddress.setText("店铺地址："+orderBean.getShopaddress());
                    suggess.setText("建议送货时间："+orderBean.getSuggesttime());
                    username.setText("下单用户名称："+orderBean.getUsername());
                    ordertime.setText("下单时间："+orderBean.getOrdertime());
                    comment.setText("评论内容："+orderBean.getContent());
                    if(orderBean.getContent()==null) {
                        add.setVisibility(View.VISIBLE);
                        change.setVisibility(View.GONE);
                        delete.setVisibility(View.GONE);
                    }else{
                        add.setVisibility(View.GONE);
                        change.setVisibility(View.VISIBLE);
                        delete.setVisibility(View.VISIBLE);
                    }
                }
                else
                {
                    Toast.makeText(Orderdetail.this,"获取资源失败",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<List<OrderBean>> call, Throwable t) {
                Toast.makeText(Orderdetail.this,"请检查网络连接",Toast.LENGTH_SHORT).show();
            }
        });

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.orderdetailback:
                finish();
                break;
            case R.id.addcomment:
                Model model1=new Model();
                Call<Success> call1= model1.insertComment(orderBean.getOrder_id(),scanf.getText().toString());
                call1.enqueue(new Callback<Success>() {
                    @Override
                    public void onResponse(Call<Success> call, Response<Success> response) {
                        if(response.isSuccessful()&&response.body()!=null)
                        {
                            if(response.body().getSuccess().equals("1")){
                                Toast.makeText(Orderdetail.this,"评论成功",Toast.LENGTH_SHORT).show();
                                comment.setText("评论内容："+scanf.getText().toString());
                                add.setVisibility(View.GONE);
                                change.setVisibility(View.VISIBLE);
                                delete.setVisibility(View.VISIBLE);
                            }else {
                                Toast.makeText(Orderdetail.this,"评论失败",Toast.LENGTH_SHORT).show();
                            }
                        }
                        else
                        {
                            Toast.makeText(Orderdetail.this,"获取资源失败",Toast.LENGTH_SHORT).show();
                        }
                    }
                    @Override
                    public void onFailure(Call<Success> call, Throwable t) {
                        Toast.makeText(Orderdetail.this,"请检查网络连接",Toast.LENGTH_SHORT).show();
                    }
                });
                break;
            case R.id.changecomment:
                Model model2=new Model();
                Call<Success> call2= model2.updateComment(orderBean.getOrder_id(),scanf.getText().toString());
                call2.enqueue(new Callback<Success>() {
                    @Override
                    public void onResponse(Call<Success> call, Response<Success> response) {
                        if(response.isSuccessful()&&response.body()!=null)
                        {
                            if(response.body().getSuccess().equals("1")){
                                Toast.makeText(Orderdetail.this,"评论成功",Toast.LENGTH_SHORT).show();
                                comment.setText("评论内容："+scanf.getText().toString());
                                add.setVisibility(View.GONE);
                                change.setVisibility(View.VISIBLE);
                                delete.setVisibility(View.VISIBLE);
                            }else {
                                Toast.makeText(Orderdetail.this,"评论失败",Toast.LENGTH_SHORT).show();
                            }
                        }
                        else
                        {
                            Toast.makeText(Orderdetail.this,"获取资源失败",Toast.LENGTH_SHORT).show();
                        }
                    }
                    @Override
                    public void onFailure(Call<Success> call, Throwable t) {
                        Toast.makeText(Orderdetail.this,"请检查网络连接",Toast.LENGTH_SHORT).show();
                    }
                });
                break;
            case R.id.deletecomment:
                Model model3=new Model();
                Call<Success> call3= model3.deleteComment(orderBean.getOrder_id());
                call3.enqueue(new Callback<Success>() {
                    @Override
                    public void onResponse(Call<Success> call, Response<Success> response) {
                        if(response.isSuccessful()&&response.body()!=null)
                        {
                            if(response.body().getSuccess().equals("1")){
                                Toast.makeText(Orderdetail.this,"删除成功",Toast.LENGTH_SHORT).show();
                                comment.setText("评论内容：null");
                                add.setVisibility(View.VISIBLE);
                                change.setVisibility(View.GONE);
                                delete.setVisibility(View.GONE);
                            }else {
                                Toast.makeText(Orderdetail.this,"删除失败",Toast.LENGTH_SHORT).show();
                            }
                        }
                        else
                        {
                            Toast.makeText(Orderdetail.this,"获取资源失败",Toast.LENGTH_SHORT).show();
                        }
                    }
                    @Override
                    public void onFailure(Call<Success> call, Throwable t) {
                        Toast.makeText(Orderdetail.this,"请检查网络连接",Toast.LENGTH_SHORT).show();
                    }
                });
                break;
        }
    }
}
